package com.bbs.gui.action.moderator;

import com.bbs.db.pers.Boardinfo;
import com.bbs.gui.form.MLoginForm;
import com.bbs.gui.iface.ModeratorIface;
import com.bbs.db.pers.Moderinfo;
import com.bbs.db.pers.Userinfo;
import com.bbs.logic.iface.BbsService;
import com.bbs.logic.iface.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @time Nov 14, 2009, 7:11:43 AM
 * @author fdcube
 */
public class MAction extends ActionSupport implements ModeratorIface {

    private Map session = ActionContext.getContext().getSession();
    private MLoginForm mloginForm;

    public MLoginForm getMloginForm() {
        return mloginForm;
    }

    public void setMloginForm(MLoginForm mloginForm) {
        this.mloginForm = mloginForm;
    }
    public List<Boardinfo> bList;

    public List<Boardinfo> getbList() {
        return bList;
    }

    public void setbList(List<Boardinfo> bList) {
        this.bList = bList;
    }
    private BbsService bbsService;

    public BbsService getBbsService() {
        return bbsService;
    }

    public void setBbsService(BbsService bbsService) {
        this.bbsService = bbsService;
    }
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public String execute() {
        try {
            bList = bbsService.GetBoard(new Boardinfo(Integer.parseInt(ActionContext.getContext().getSession().get("bid").toString())));
            System.out.println("长度：" + bList.size());
            return "loginok";
        } catch (Exception e) {
            System.out.println(e);
            bList = bbsService.GetBoards();
            return SUCCESS;
        }
    }

    public String Login() {
        List<Userinfo> utmp = null;
        try {
            utmp = userService.GetUser(new Userinfo(mloginForm.getUname(), mloginForm.getUpwd()));
            for (Iterator<Moderinfo> tmp = utmp.get(0).getModerinfos().iterator(); tmp.hasNext();) {
                if (tmp.next().getBoardinfo().getBid() == Integer.parseInt(mloginForm.getBid())) {
                    session.put("bid", mloginForm.getBid());
                    bList = bbsService.GetBoard(new Boardinfo(Integer.parseInt(mloginForm.getBid())));
                    System.out.println(bList);
                    return "loginok";
                }
            }
            bList = bbsService.GetBoards();
            this.addActionError("您没有管理该板块的权限！");
            return ERROR;
        } catch (Exception e) {
            bList = bbsService.GetBoards();
            this.addActionError("登录失败，用户名或密码错误！" + e);
            return ERROR;
        }
    }

    public String Logout() {
        try {
            session.clear();
            return "logout";
        } catch (Exception e) {
            return ERROR;
        }
    }
}
